package cn.kmia.hrm.action;

import cn.kmia.hrm.pojo.PostInfo;
import cn.kmia.hrm.pojo.system.User;
import cn.kmia.hrm.service.PostInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Scope;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.util.List;

/**
 * @program: hrm
 * @description: 岗位信息操作
 * @author: 4K
 * @create: 2018-07-19 16:29
 **/
@IocBean
@At("/postInfo")
public class PostInfoAction {

    private static final Log log = Logs.get();

    @Inject
    protected PostInfoService postInfoService;

    /**
     * 新增
     *
     * @param postInfo 岗位信息
     * @param user     操作者
     * @return
     */
    @At("/add")
    @Ok("json:full")
    @RequiresPermissions("postInfo:add")
    public Object add(@Param("..") PostInfo postInfo, @Attr(scope = Scope.SESSION, value = "user") User user) {
        NutMap nm = new NutMap();
        postInfo = postInfoService.add(postInfo, user.getUser_idcard());
        if (postInfo == null) {
            nm.setv("success", false).setv("errorMsg", "创建岗位失败!");
        } else {
            nm.setv("success", true).setv("obj", postInfo);
        }
        return nm;
    }

    /**
     * 更新
     *
     * @param postInfo 岗位信息
     * @param user
     * @return
     */
    @At("/update")
    @Ok("json:full")
    @RequiresPermissions("postInfo:update")
    public Object update(@Param("..") PostInfo postInfo, @Attr(scope = Scope.SESSION, value = "user") User user) {
        NutMap re = new NutMap();
        try {
            if (postInfoService.update(postInfo, user.getUser_idcard())) {
                re.setv("success", true);
            } else {
                re.setv("success", false);
            }
        } catch (Exception e) {
            re.setv("success", false);
            log.error("更新异常", e);
        }
        return re;
    }

    /**
     * 删除
     *
     * @param ids int[]
     * @return
     */
    @At("/del")
    @Ok("json:full")
    @RequiresPermissions("postInfo:del")
    public Object del(@Param("ids") int[] ids) {
        NutMap re = new NutMap();
        try {
            if (ids != null) {
                re = postInfoService.del(ids);
                re.setv("success", true);
            } else {
                re.setv("success", false);
            }
        } catch (Exception e) {
            re.setv("success", false);
            log.error("删除异常", e);
        }
        return re;
    }

    /**
     * 分页显示
     *
     * @param pager 分页
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("postInfo:queryByPage")
    public Object queryByPage(@Param("..") Pager pager, @Param("verify_status") String verify_status,@Param("name") String postInfo_name, @Param("floor") int floor) {
        NutMap nm = new NutMap();
        QueryResult qr = postInfoService.queryByPage(postInfo_name,verify_status, floor, pager);
        nm.setv("rows", qr.getList());
        nm.setv("total", qr.getPager().getRecordCount());
        return nm;
    }

    /**
     * 查询
     *
     * @return
     */
    @At("/query")
    @Ok("json:full")
    public List<PostInfo> query(@Param("floor") int floor) {
        return postInfoService.query(floor);
    }

    /**
     * 管理查询
     *
     * @return
     */
    @At("/queryManage")
    @Ok("json:full")
    public List<PostInfo> queryManage(@Param("floor") int floor) {
        return postInfoService.queryManage(floor);
    }

    /**
     * 查询名称是否存在
     *
     * @param postInfo_name
     * @return false 存在 true 不存在
     */
    @At("/find")
    @Ok("json:full")
    public Object find(@Param("name") String postInfo_name) {
        NutMap re = new NutMap();
        try {
            if (postInfoService.checkPostInfo(postInfo_name)) {
                re.setv("valid", false);
            } else {
                re.setv("valid", true);
            }
        } catch (Exception e) {
            re.setv("exist", false);
            log.error("查询名称是否存在异常", e);
        }
        return re;
    }

    /**
     * 审核
     *
     * @param ids int[]
     * @return
     */
    @At("/verify")
    @Ok("json:full")
    @RequiresPermissions("postInfo:verify")
    public Object verify(@Param("ids") int[] ids,@Param("verify_status") String verify_status,@Attr(scope = Scope.SESSION, value = "user") User user) {
        NutMap re = new NutMap();
        try {
            if (ids != null) {
                re = postInfoService.verify(ids,verify_status,user.getUser_idcard());
                re.setv("success", true);
            } else {
                re.setv("success", false);
            }
        } catch (Exception e) {
            re.setv("success", false);
            log.error("审核异常", e);
        }
        return re;
    }
}
